package com.example.grid.webserviceclient;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.ksoap2.serialization.PropertyInfo;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;



/**
 * Created by liuyi on 17/10/7.
 */

public class CallSSLWebServiceActivity extends AppCompatActivity implements View.OnClickListener {
    private static String TAG = "CallSSLWebService";
    private EditText mEdtXmlPara,mEdtResult;
    private ProgressDialog mProgressDialog = null;
    private String mSelectedMethodName,mXmlParam;

    public  static String mJson="{\"PERSON_INDEX\":\"009009009\",\"ID\":\"04faa3db-dff4-4a8e-8b5f-ba55483dab0b\",\"SCREENING_STATUS\":\"1\",\"SCREENING_DATE\":\"\",\"REFERRAL_DATE\":\"2016-6-24\",\"REFERRAL_UNIT\":\"4509020016\",\n" +
            "\"REPORT_DATE\":\"2016-6-24\",\"TREAT_DATE\":\"2016-6-24\",\"BBTNZ_RESULT\":\"0\",\"BBTNZ_VALUE\":\"1\",\"BBTNZ_CONSULT_H\":\"2\",\"BBTNZ_LABORATORY\":\"\",\"XTXJD_RESULT\":\"0\",\"XTXJD_VALUE\":\"1\",\"XTXJD_CONSULT_H\":\"8\",\n" +
            "\"XTXJD_LABORATORY\":\"\",\"G60D_RESULT\":\"0\",\"THAL_SCREENING\":\"9\",\"CONGENITAL_SCREENING\":\"0\",\"TANDEM_SCREENING\":\"1\",\"BBTNZ_DIAGNOSE\":\"1\",\"BBTNZ_BBTNZ_LABORATORY_Z\":\"\",\"CLASSICAL_PKU\":\n" +
            "\"\",\"PHENYLALANINE\":\"\",\"CONGENITAL_DIAGNOSE\":\"1\",\"CONGENIT_LABORATORY_Z\":\"\",\"HYPOTHYROIDISM\":\"\",\"HIGHT_TSH\":\"\",\"DIAGNOSE_DATE\":\"2016-6-24\",\"PKU_TREAT\":\"0\",\"PKU_TREAT_LAB\":\"\",\n" +
            "\"HYPERPHENY_TREAT\":\"0\",\"HYPERPHENY_TREAT_LAB\":\"\",\"HYPOTHY_TREAT\":\"0\",\"HYPOTHY_TREAT_LAB\":\"\",\"HIGHT_TSH_TREAT\":\"0\",\"HIGHT_TSH_TREAT_LAB\":\"\",\"DIAG_REPORT_DATE\":\"\",\"BBTNZ_CONSULT_L\":\"\",\"XTXJD_CONSULT_L\":\"\",\n" +
            "\"G60D_RESULT_VALUE1\":\"1\",\"G60D_RESULT_CONSULT_H\":\"0.2\",\"G60D_RESULT_CONSULT_L\":\"\",\"THAL_SCREENING_ID\":\"\",\"CONGENITAL_RESULT\":\"1\",\"CONGENITAL_CONSULT_H\":\"30\",\n" +
            "\"CONGENITAL_CONSULT_L\":\"\",\"TANDEM_SCREENING_CON\":\"c1da8f82-5841-4a2a-89e8-777ac82f7c81\",\"BLOODTIME\":\"2016-6-24\",\"BLOOD_NO\":\"采血卡号\",\"SAMPL_CLASS\":\"2\",\"SAMPLE_PROP\":\"1\",\"INP_NO\":\"123\",\n" +
            "\"BLOOD_OPERATOR\":\"采血人\",\"DOCTOR\":\"送检医生\",\"FEELIST\":\"1\",\"CHECK_UNIT_CODE\":\"4501030273\",\"CHECK_UNIT_NAME\":\"广西壮族自治区卫生计生统计信息中心\",\"FEENO\":\"三联单号\",\"SAVE_VISION\":\"1.4.2.8\",\n" +
            "\"userCode\":\"USR002459141231\",\"unitCode\":\"4501030273\"}";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_callsslwebserviceactivity);
        Intent intent = getIntent();
        mSelectedMethodName = intent.getStringExtra("methodName");
        mXmlParam = intent.getStringExtra("xmlParam");
        initView();
        rxJavaCallWebService();
    }

    private void initView()
    {
        mEdtXmlPara = (EditText)findViewById(R.id.edtXmlPara);
        mEdtResult = (EditText)findViewById(R.id.edtResult);
        mEdtXmlPara.setKeyListener(null);
        mEdtResult.setKeyListener(null);
        mEdtXmlPara.setText(mXmlParam);
        ((Button)findViewById(R.id.btn_doCallSSLWebSv)).setText("调用"+mSelectedMethodName);
        findViewById(R.id.btn_doCallSSLWebSv).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        rxJavaCallWebService();
    }


    private String getThrowableDetail(Throwable e) {
        StringBuffer stringBuffer = new StringBuffer(e.toString() + "\n");
        StackTraceElement[] messages = e.getStackTrace();
        int length = messages.length;
        for (int i = 0; i < length; i++) {
            stringBuffer.append("\t"+messages[i].toString()+"\n");
        }
        return stringBuffer.toString();
    }


    private void rxJavaCallWebService() {
        mEdtResult.setText("");
        if (mProgressDialog == null) {
            mProgressDialog = ProgressDialog.show(this, "Requsting...", "Please wait...", true, false);
        } else {
            mProgressDialog.show();
        }
       // SslRequest.allowAllSSL(WebServiceHelper.getUrl(),this);//ssl验证
        Observable<Object> observable = Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                try {
                    List<PropertyInfo>list = PropertyListFactory.getPropertyList(mSelectedMethodName,mXmlParam);
                    emitter.onNext(WebServiceHelper.requestWebService(mSelectedMethodName,list));
                    emitter.onComplete();
                } catch (Exception e) {
                    emitter.onError(e);
                }

            }
        });

        Observer<Object> observer = new Observer<Object>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(Object value) {
                if (value != null) {
                    mEdtResult.setText("onNext:"+value.toString());
                }
            }

            @Override
            public void onError(Throwable e) {
                if (e !=null) {
                    mEdtResult.setText("Throwable:"+e.toString()+"\n"+getThrowableDetail(e));
                }
                if (mProgressDialog != null) {
                    mProgressDialog.dismiss();
                }
            }

            @Override
            public void onComplete() {
                if (mProgressDialog != null) {
                    mProgressDialog.dismiss();
                }
            }
        };

        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }


}
